<?php
namespace WinZ\DataMinerBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpKernel\Exception\HttpException;
use WinZ\DataMinerBundle\Entity\Character;
use WinZ\DataMinerBundle\Entity\Server;
use WinZ\DataMinerBundle\Entity\Session;
use WinZ\DataMinerBundle\Entity\Node;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of PageController
 *
 * @author Winz
 */

class RemoteController extends Controller
{
    public function updateCharacterAction($name = "", $server ="")
    {
        
        $request=$this->getRequest();
        $content2 = $request->getContent();
        $c = json_decode($content2,true);
//        $c = json_decode($request->getContent(),true);

        if($name && $server) {
            $c['name'] ="Twolay";
            $c['server'] ="Uldaman";
            $c['gold'] ="67883";
            $c['nodes'] ="0";
            $c['sessionId'] = "";
        }        
/*
 * {"name":"Twolay", "server":"Uldaman", "gold":"67883", "sessionId":"", "nodes":"0"}
        $jsonTest = '{"name":"Dro\u00f8d\u00eex", "server":"Hyjal", "gold":"4726", "sessionId":"", "nodes":"86"}';
        $c = json_decode($jsonTest,true);
*/

        if( !(isset($c["sessionId"]) && $c["sessionId"])  && !($c["name"] && $c["server"]) )
            return \Symfony\Component\HttpFoundation\Response::create(json_encode("Error : you must pass either SessionId, either name and server in JSON format"),500);
        if( $c["server"] == "unknown")
            return \Symfony\Component\HttpFoundation\Response::create(json_encode("Error : server was set to Unknown"),500);

        
        $em = $this->getDoctrine()
           ->getEntityManager();

        /*
        if($c["sessionId"]) {
           $session = $em->getRepository('DataMinerBundle:Session')->findBy(array("id"=>$c['sessionId']));
        }
        else */
        if($c['server'] && $c['name']) {
            $serv = $em->getRepository('DataMinerBundle:Server')->getByName($c['server']);
            $char = $em->getRepository('DataMinerBundle:Character')->getByNameServer($c['name'],$serv);
            
            $session = $em->getRepository('DataMinerBundle:Session')->getByCharacterCurrent($char);
            $em->persist($session);
        } 
        
        if(isset($c['nodes']) && $c['nodes']) {
            $node = new Node();
            $node->setQty($c['nodes'])
                 ->setSession($session);
            $em->persist($node);
            $session->getCharacter()->setLastGather($node->getDt());
        }
        
        if(isset($c['gold']))
            $session->getCharacter()->setGold($c['gold']);
        if(isset($c['status']))
            $session->setStatus($c['status']);
        if(isset($c['goal']))
            $session->setGoal($c['goal']);
        
        $session->setDtEnd(new \DateTime());
        
        $em->flush();


/*            $session = new Session();
            $session->setCharacter($char);
            $em->persist($session);
            $em->flush();
 */       
            $resp = array(
            'response' => 'success',
            'content' => $request->getContent(),
            'contentType'=> $request->getContentType(),
            'format'=>$request->getRequestFormat(),
            'name'=> $session->getCharacter()->getName(),
            'sessionId'=> $session->getId()
                    );
                   

        return \Symfony\Component\HttpFoundation\Response::create(json_encode($resp));
    }

}
?>
